<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="form">
			<u-form :model="model" :rules="rules" ref="uForm" :errorType="errorType">
			    <u-form-item :label-position="labelPosition" label="手机号" prop="mobile" label-width="150">
			        <u-input placeholder="请输入手机号" v-model="model.mobile" type="text"></u-input>
			    </u-form-item>
				<u-form-item :label-position="labelPosition" label="密码" prop="password" label-width="150">
					<u-input :password-icon="true" type="password" v-model="model.password" autoComplete="off" placeholder="请输入密码"></u-input>
				</u-form-item>
			</u-form>
			<u-button :disabled="!isCanSubmit" :style="[inputStyle]" class="button" type="button" @click="submit">登录</u-button>
			<view class="fBtn">
				<view class="registBtn" @click="goRegist">注册账号</view>
				<view class="separate">|</view>
				<view class="forgotBtn">忘记密码</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState, mapGetters, mapActions } from 'vuex'
	export default {
		data() {
			return {
				isCanSubmit: true,
			    labelPosition: 'left',
			    errorType: ['message'],
			    model: {
					mobile: '18888888888',
					password: '123456',
				},
			    rules: {
					mobile: [
						{
							required: true,
							message: '请输入手机号',
							trigger: ['change','blur'],
						},
						{
							validator: (rule, value, callback) => {
								// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change，二者之间用英文逗号隔开
							trigger: ['change','blur'],
						}
					],
					password: [
						{
							required: true,
							message: '请输入密码',
							trigger: ['change','blur'],
						},
						// {
						//  // 正则不能含有两边的引号
						//  pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]+\S{5,12}$/,
						//  message: '需同时含有字母和数字，长度在6-12之间',
						//  trigger: ['change','blur'],
						// }
						{
							min: 6,
							max: 16,
							message: '密码长度在6到16个字符',
							trigger: ['change','blur'],
						},
					],
			    }
			}
		},
		onLoad() {

		},
		computed: {
			...mapGetters(['isLogin']),
			...mapState(['loginInfo', 'userInfo']),
			inputStyle() {
				let style = {};
				if (this.model.mobile) {
					style.color = "#fff";
					style.backgroundColor = this.$u.color['warning'];
				}
				return style;
			}
		},
		onReady() {
		    this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			...mapActions(['loginAction', 'updateUserInfo']),
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						this.getLogin()
					} else {
						console.log('验证失败');
					}
				});
			},
			getLogin() {
				this.isCanSubmit = false
				
				uni.getSystemInfo({
					success: res => {
						this.loginAction({ mobile: this.model.mobile, password: this.model.password, device_id: res.deviceId }).then(async val => {
							if (val) {
								setTimeout(async () => {
									await this.updateUserInfo()
									this.$ws.checkOpenSocket()
									this.goMessage()
								}, 1000)
							}
						})
					},
					fail() {
						this.$u.toast('获取系统信息失败！')
					}
				})
				
				setTimeout(() => {
					this.isCanSubmit = true
				}, 2000)
			},
			goMessage() {
				setTimeout(() => {
					this.$u.route({
						url: 'pages/message/message',
						type: 'reLaunch'
					})
				}, 1000)
			},
			goRegist() {
				this.$u.route('/pages/auth/register/register')
			}
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}
	.form {
		margin-top: 80rpx auto;
		width: 580rpx;
		.button {
			margin-top: 20px;
			background-color: rgb(253, 243, 208);
			color: $u-tips-color;
			border: none;
			&::after {
				border: none;
			}
		}
		.fBtn {
			text-align: center;
			color: #333333;
			font-size: 15px;
			margin-top: 20rpx;
			display: flex;
			.registBtn {
				text-align: right;
				width: 340rpx;
			}
			.forgotBtn {
				text-align: left;
				width: 340rpx;
			}
			.separate {
				width: 70rpx;
			}
		}
	}
	
</style>